﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Infrastructure.Dao;
using DormMS.IService;
using DormMS.Model;
using SqlSugar;
using DormMS.Model.ViewModels;

namespace DormMS.Service
{
    public class CommonAreaService : ICommonAreaService
    {
        public Common_Area GetArea(int Id)
        {
            using (var _db = DbConfig.GetInstance())
            {
                _db.DisableUpdateColumns = new string[] { "" };
                _db.CurrentFilterKey = "";
                return _db.Queryable<Common_Area>().SingleOrDefault(m => m.Id == Id);
            }
        }

        public Common_Area GetArea(string name, int level)
        {
            using (var _db = DbConfig.GetInstance())
            {
                _db.DisableUpdateColumns = new string[] { "" };
                _db.CurrentFilterKey = "";
                return _db.Queryable<Common_Area>().SingleOrDefault(m => m.Name == name && m.Level == level);
            }
        }

        public List<Common_Area> GetAreaList(int parentId)
        {
            using (var _db = DbConfig.GetInstance())
            {
                _db.DisableUpdateColumns = new string[] { "" };
                _db.CurrentFilterKey = "";
                return _db.Queryable<Common_Area>().Where(m => m.ParentId == parentId).ToList();
            }
        }

        public List<Common_Area> GetAreaList()
        {
            using (var _db = DbConfig.GetInstance())
            {
                _db.DisableUpdateColumns = new string[] { "" };
                _db.CurrentFilterKey = "";
                return _db.Queryable<Common_Area>().Where(m => m.Id > 0).ToList();
            }
        }

        public List<Common_Area> GetAreaListByIds(List<int> idlist)
        {
            using (var _db = DbConfig.GetInstance())
            {
                _db.DisableUpdateColumns = new string[] { "" };
                _db.CurrentFilterKey = "";
                return _db.Queryable<Common_Area>().In(m => m.Id, idlist).ToList();
            }
        }


        public List<AreaModel> GetAreaTreeDate(int pid)
        {
            using (var db = DbConfig.GetInstance())
            {
                db.CurrentFilterKey = "";
                var alllist = db.Queryable<Common_Area>().Where(t => t.Level < 3).OrderBy("Id,Sort").ToList();
                var currentAuth = db.Queryable<Product_Area>().Where(t => t.ProductId == pid).ToList();
                List<AreaModel> list = new List<AreaModel>();
                foreach (var item in alllist)
                {
                    AreaModel area = new AreaModel();
                    area.Id = item.Id;
                    area.ParentId = item.ParentId;
                    area.text = item.Name;
                    area.Sort = item.Sort;
                    if (currentAuth != null && currentAuth.Count > 0)
                    {
                        var cc = currentAuth.FirstOrDefault(t => t.AreaId == item.Id);
                        if (cc != null)
                        {
                            area.state = new { selected = true };
                        }
                    }

                    list.Add(area);

                }

                return list;
            }
        }

    }
}
